<template>
  <!-- 区域价格管理 -->
  <div>
    <div class="app-page">
      <org-tree
        class="orgTree"
        title="组织机构"
        :tree-data="orgTreeData"
        :mouse-button="false"
        :loading="$loadingPlugin.getOrgData"
        @currentChange="orgChange"
      />
      <div class="right">
        <div style="margin-bottom: 10px;" class="btn-group">
          <div>
            <el-checkbox v-model="checked" style="margin-right: 20px;" @change="checkboxChange">包含下级</el-checkbox>
            <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="add">新增</el-button>
          </div>
        </div>
        <lb-table
          ref="lbTable"
          v-el-height-adaptive-table="{bottomOffset:10}"
          class="table"
          border
          highlight-current-row
          element-loading-text="加载中"
          :column="tableData.column"
          :data="tableData.data"
          :pagination="false"
          :immediate-fetch="false"
          @fetch="fetchData"
        />
      </div>
    </div>
    <add ref="add" />
  </div>
</template>

<script>
  import { querySendTimeLimitList, sendTimeLimitDelete } from '@/api'
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'
  import moment from 'dayjs'
  import OrgTree from '../../components/OrgTree'
  import Add from './components/Add'

  export default {
    name: 'limitConfigSetting',
    components: {
      OrgTree, Add
    },
    data() {
      return {
        checked: false,
        orgTreeData: [],
        currentOrg: null,
        tableData: {
          total: 0,
          column: [
            {
              prop: 'orgName',
              label: '机构'
            },
            {
              prop: 'beginMonth',
              label: '月份',
              render: (h, { row }) => <span>{row.beginMonth} ~ {row.endMonth}月</span>
            },
            {
              prop: 'startTime',
              label: '配送时段',
              render: (h, { row }) => <span>{moment(row.beginTime).format('HH:mm')} ~ {moment(row.endTime).format('HH:mm')}</span>
            },
            {
              prop: 'warnMinutes',
              label: '预警时间(分钟)',
              align: 'right'
            },
            {
              prop: 'maxMinutes',
              label: '超时时间(分钟)',
              align: 'right'
            },
            {
              label: '操作',
              align: 'center',
              width: 50,
              render: (h, { row, $index }) => {
                return (
                  <div>
                    <el-link size='mini' underline={false} type='danger' onClick={() => this.del(row, $index)}>
                      删除
                    </el-link>
                  </div>
                )
              }
            }
          ],
          data: []
        }
      }
    },
    computed: {
      ...mapGetters(['orgList'])
    },
    created() {
      this.getOrgData()
    },
    methods: {
      async fetchData() {
        const query = {
          currentPage: 1,
          pageSize: 999,
          orgId: this.currentOrg.orgId,
          selectAll: 0
        }
        if (this.checked) query.selectAll = 1
        const res = await querySendTimeLimitList(query)
        this.tableData.data = res.data.rows
      },
      checkboxChange(val) {
        this.checked = val
        this.$refs.lbTable.fetch()
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      async add() {
        await this.$refs.add.open(this.currentOrg)
        this.$refs.lbTable.fetch()
      },
      // 删除报价
      del(row, index) {
        this.$confirm('确定删除此条数据？', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$message({
            message: '正在删除',
            duration: 0,
            iconClass: 'el-icon-loading'
          })
          sendTimeLimitDelete({ limitId: row.limitId }).then(() => {
            this.tableData.data.splice(index, 1)
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            loading.close()
          })
        })
      },
      // 获取组织机构数据
      async getOrgData() {
        const data = this.orgList.map(item => ({
          label: item.orgName,
          ...item
        }))
        this.orgTreeData = getTreeData(data)
      },
      // 选择组织机构变化
      orgChange(current) {
        this.currentOrg = current
        if (this.currentOrg) this.fetchData()
      }
    }
  }
</script>

<style lang='scss' scoped>
.app-page {
  display: flex;
  height: calc(100vh - 125px);
  padding: 0;
  .right {
    width: calc(100% - 270px);
    margin-left: 10px;
  }
}
</style>
